package BanyuanClub.Study.Day0319.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XML {
    public static void main(String[] args) throws Exception {
        File xmlFile = new File("javase/xml/Book.xml");
        SAXReader reader = new SAXReader();
        Document xmlDoc = reader.read(xmlFile);

        Element bookstoreEl = xmlDoc.getRootElement();

        List<Element> listEL = bookstoreEl.elements();

        List<Book> bookslist = new ArrayList<>();

        for (Element bookEL : listEL
             ) {
            String isbn = bookEL.attributeValue("isbn");
            String title = bookEL.elementText("title");
            String author = bookEL.elementText("author");
            Integer price = Integer.parseInt(bookEL.elementText("price"));
            bookslist.add(new Book(isbn,title,author,price));
        }
        System.out.println(bookslist);
    }
}
